[gcode_macro _SET_TOOL_FAN]
variable_speed: 0
gcode:
    SET_FAN_SPEED FAN=fan_{printer.toolhead.extruder} SPEED={params.SPEED}      # Установка скорости вентилятора определённого экструдера
    # Store the fan speed so that we can carry it over during toolchanges.
    SET_GCODE_VARIABLE MACRO=_SET_TOOL_FAN VARIABLE=speed VALUE={params.SPEED}  # Сохранить этого значение скорости

[gcode_macro M106]
description: Start the corresponding model blowing fan.
# Only rename_existing if you have a sacrificial [fan] section
#rename_existing: M106.1
# The variable that controls fan speed swopping if not specifying P parameter
# -1 means the control is disabled, a value of 0-1 is the requested fan speed.
# Access via {printer["gcode_macro M106"].swap_speed}
variable_swap_speed: -1
gcode:
    {% set s = [[params.S|default(255)|int, 255]|min, 0]|max %}
    {% set p = params.P|default(-1)|int %}
    {% set speed = s / 255 %}

    # Set speed to -1 by default
    SET_GCODE_VARIABLE MACRO=M106 VARIABLE=swap_speed VALUE=-1

    {% if p == -1 %}
      # Set current active extruder fan
      {% if speed == 0 %}
        # Always turn off al fans if S0 is specified without a specific fan
        SET_FAN_SPEED FAN=fan_extruder SPEED=0
        SET_FAN_SPEED FAN=fan_extruder1 SPEED=0
      {% else %}
        # Opt into fan speed swop control
        {% if printer.dual_carriage.mode == "FULL_CONTROL" %}
          SET_GCODE_VARIABLE MACRO=M106 VARIABLE=swap_speed VALUE={speed}
          SET_FAN_SPEED FAN=fan_{printer.toolhead.extruder} SPEED={speed}
        {% else %}
          SET_FAN_SPEED FAN=fan_extruder SPEED={speed}
          SET_FAN_SPEED FAN=fan_extruder1 SPEED={speed}
        {% endif %}
      {% endif %}
    {% else %}
      # Set specified active extruder fan
      {% if p == 0 %}
        SET_FAN_SPEED FAN=fan_extruder SPEED={speed}
      {% else %}
        SET_FAN_SPEED FAN=fan_extruder1 SPEED={speed}
      {% endif %}
    {% endif %}

    # Update core Klipper's fan speed
    # Only do this if you have a sacrificial [fan] section
#    M106.1 S{s}

